using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VCUBEST
    {
        public DataRow Convert2CUBEST(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryCUBEST(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["CUEMANDANT"] = " ".PadRight(2 ); 
         dwReturn["CUEAUFTNR"] = " ".PadRight(11 ); 
         dwReturn["CUENEUB"] = " ".PadRight(8 ); 
         dwReturn["CUENEUD"] = " ".PadRight(0 ); 
         dwReturn["CUEAENB"] = " ".PadRight(8 ); 
         dwReturn["CUEAEND"] = " ".PadRight(0 ); 
         dwReturn["CUEAENZL"] = " ".PadRight(0 ); 
         dwReturn["CUEKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["CUEDKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["CUEDWERK"] = " ".PadRight(3 ); 
         dwReturn["CUEDABT"] = " ".PadRight(3 ); 
         dwReturn["CUEDART"] = " ".PadRight(20 ); 
         dwReturn["CUEDVAR"] = " ".PadRight(2 ); 
         dwReturn["CUEDZUSAETZE"] = " ".PadRight(40 ); 
         dwReturn["CUEANZSTK"] = " ".PadRight(0 ); 
         dwReturn["CUESTKLG"] = " ".PadRight(0 ); 
         dwReturn["CUESTKLGV"] = " ".PadRight(0 ); 
         dwReturn["CUESTKLGB"] = " ".PadRight(0 ); 
         dwReturn["CUEAUFMSL"] = " ".PadRight(2 ); 
         dwReturn["CUELIEFDAT"] = " ".PadRight(0 ); 
         dwReturn["CUELO"] = " ".PadRight(2 ); 
         dwReturn["CUELB"] = " ".PadRight(2 ); 
         dwReturn["CUELP"] = " ".PadRight(8 ); 
         dwReturn["CUEAUFBKZ"] = " ".PadRight(1 ); 
         dwReturn["CUEFREIG"] = " ".PadRight(1 ); 
         dwReturn["CUEFILLER"] = " ".PadRight(93 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapCUBEST(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveCUBEST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryCUBEST(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapCUBEST(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertCUBEST(dwLocal, ITXDBVERSION);
            }
            UpdateCUBEST(dwLocal);
        }
        public void InsertCUBEST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO CUBEST(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryCUBEST(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM CUBEST WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM CUBEST  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "CUBEST";
            Return = Result;
            return Return;
        }
        public void DeleteCUBEST(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM CUBEST   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateCUBEST(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE CUBEST SET "
                    +"CUEMANDANT= :CUEMANDANT,"
                    +"CUEAUFTNR= :CUEAUFTNR,"
                    +"CUENEUB= :CUENEUB,"
                    +"CUENEUD= :CUENEUD,"
                    +"CUEAENB= :CUEAENB,"
                    +"CUEAEND= :CUEAEND,"
                    +"CUEAENZL= :CUEAENZL,"
                    +"CUEKUNDNR= :CUEKUNDNR,"
                    +"CUEDKUNDNR= :CUEDKUNDNR,"
                    +"CUEDWERK= :CUEDWERK,"
                    +"CUEDABT= :CUEDABT,"
                    +"CUEDART= :CUEDART,"
                    +"CUEDVAR= :CUEDVAR,"
                    +"CUEDZUSAETZE= :CUEDZUSAETZE,"
                    +"CUEANZSTK= :CUEANZSTK,"
                    +"CUESTKLG= :CUESTKLG,"
                    +"CUESTKLGV= :CUESTKLGV,"
                    +"CUESTKLGB= :CUESTKLGB,"
                    +"CUEAUFMSL= :CUEAUFMSL,"
                    +"CUELIEFDAT= :CUELIEFDAT,"
                    +"CUELO= :CUELO,"
                    +"CUELB= :CUELB,"
                    +"CUELP= :CUELP,"
                    +"CUEAUFBKZ= :CUEAUFBKZ,"
                    +"CUEFREIG= :CUEFREIG,"
                    +"CUEFILLER= :CUEFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("CUEMANDANT", OracleType.NVarChar).Value = dwData["CUEMANDANT"].ToString(); 
          or1.Parameters.Add("CUEAUFTNR", OracleType.NVarChar).Value = dwData["CUEAUFTNR"].ToString(); 
          or1.Parameters.Add("CUENEUB", OracleType.NVarChar).Value = dwData["CUENEUB"].ToString(); 
          or1.Parameters.Add("CUENEUD", OracleType.NVarChar).Value = dwData["CUENEUD"].ToString(); 
          or1.Parameters.Add("CUEAENB", OracleType.NVarChar).Value = dwData["CUEAENB"].ToString(); 
          or1.Parameters.Add("CUEAEND", OracleType.NVarChar).Value = dwData["CUEAEND"].ToString(); 
          or1.Parameters.Add("CUEAENZL", OracleType.NVarChar).Value = dwData["CUEAENZL"].ToString(); 
          or1.Parameters.Add("CUEKUNDNR", OracleType.NVarChar).Value = dwData["CUEKUNDNR"].ToString(); 
          or1.Parameters.Add("CUEDKUNDNR", OracleType.NVarChar).Value = dwData["CUEDKUNDNR"].ToString(); 
          or1.Parameters.Add("CUEDWERK", OracleType.NVarChar).Value = dwData["CUEDWERK"].ToString(); 
          or1.Parameters.Add("CUEDABT", OracleType.NVarChar).Value = dwData["CUEDABT"].ToString(); 
          or1.Parameters.Add("CUEDART", OracleType.NVarChar).Value = dwData["CUEDART"].ToString(); 
          or1.Parameters.Add("CUEDVAR", OracleType.NVarChar).Value = dwData["CUEDVAR"].ToString(); 
          or1.Parameters.Add("CUEDZUSAETZE", OracleType.NVarChar).Value = dwData["CUEDZUSAETZE"].ToString(); 
          or1.Parameters.Add("CUEANZSTK", OracleType.NVarChar).Value = dwData["CUEANZSTK"].ToString(); 
          or1.Parameters.Add("CUESTKLG", OracleType.NVarChar).Value = dwData["CUESTKLG"].ToString(); 
          or1.Parameters.Add("CUESTKLGV", OracleType.NVarChar).Value = dwData["CUESTKLGV"].ToString(); 
          or1.Parameters.Add("CUESTKLGB", OracleType.NVarChar).Value = dwData["CUESTKLGB"].ToString(); 
          or1.Parameters.Add("CUEAUFMSL", OracleType.NVarChar).Value = dwData["CUEAUFMSL"].ToString(); 
          or1.Parameters.Add("CUELIEFDAT", OracleType.NVarChar).Value = dwData["CUELIEFDAT"].ToString(); 
          or1.Parameters.Add("CUELO", OracleType.NVarChar).Value = dwData["CUELO"].ToString(); 
          or1.Parameters.Add("CUELB", OracleType.NVarChar).Value = dwData["CUELB"].ToString(); 
          or1.Parameters.Add("CUELP", OracleType.NVarChar).Value = dwData["CUELP"].ToString(); 
          or1.Parameters.Add("CUEAUFBKZ", OracleType.NVarChar).Value = dwData["CUEAUFBKZ"].ToString(); 
          or1.Parameters.Add("CUEFREIG", OracleType.NVarChar).Value = dwData["CUEFREIG"].ToString(); 
          or1.Parameters.Add("CUEFILLER", OracleType.NVarChar).Value = dwData["CUEFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
